<?php
namespace Lib\Compressor;
use Lib\Compressor\CompressorException,
    Lib\JSMin;
/**
 * Class description
 *
 * @since 1.0
 * @author Anton Patrikeyev <anton.patrikeyev@gmail.com>
 */
class CompressorQueue{
    /**
     * @var string
     */
    protected $_name = null;
    
    /**
     * @var \ArrayObject|null
     */
    protected $_files = null;

    /**
     * Aggregate content of queue files
     * @var string
     */
    protected $_content = '';

    /**
     * 
     */
    public function __construct($name, $files = array()){
        $this->setName($name);
        $this->_files = new \ArrayObject();
    }

    /**
     * @param  $file
     * @return
     */
    public function appendFile($file){
        if(is_dir($file)){
            $this->appendDir($file);
            return;
        } else {
            $this->_files->append($file);
        }
    }

    /**
     * @throws CompressorException
     * @todo Add custom filters
     * @param  $dir
     * @return void
     */
    public function appendDir($dir, $filter = null){
        if(!is_dir($dir)){
            throw new CompressorException('Directory '.$dir. ' doesn\'t exists');
        }
        $directoryIterator = new \DirectoryIterator($dir);

        foreach($directoryIterator as $file){
            if($directoryIterator->isFile()){
                $this->appendFile($directoryIterator->getPath() .'/'. $file);
            }
        }
    }

    /**
     * 
     */
    private function _process(){
        $this->appendContent('/* File generated on: '.date('Y-m-d H:i:s')." */\n");
        foreach ($this->_files as $file){
            if(!(is_file($file) && is_readable($file))){
                throw new CompressorException('File '. (string) $file. ' is not founded on filesystem or not readable');
            }
            $preContent = "\n".'/* FILE: '.pathinfo($file, PATHINFO_FILENAME).' */'."\n";
            $this->appendContent($preContent. file_get_contents($file));
        }
    }

    /**
     * @param string $name
     * @param string $path
     * @param string $compress
     * @return string
     */
    public function save($extension, $path, $compress = true, $minify = false){
        $this->_process();
        // Creating destination path
        $destinationName = realpath($path);
        $destinationName .= '/' . 'pack-'.$this->getName() . '.' . $extension;

        $content = $this->getContent();
        if($minify){
            $content = JSMin::minify($content);
        }

        file_put_contents($destinationName, $content);

        if($compress){
            $gzdata = gzencode($content, 9);
            file_put_contents($destinationName.'.gz', $gzdata);
        }
    }
    
    /**
     * @param  $name
     * @return void
     */
    public function setName($name) {
        $this->_name = $name;
    }

    /**
     * @return null|string
     */
    public function getName() {
        return $this->_name;
    }

    /**
     * @param  $files
     * @return void
     */
    public function setFiles($files) {
        $this->_files = $files;
    }

    /**
     * @return \ArrayObject|null
     */
    public function getFiles() {
        return $this->_files;
    }

    /**
     * @param  $content
     * @return void
     */
    public function setContent($content) {
        $this->_content = $content;
    }

    /**
     * @return string
     */
    public function getContent() {
        return $this->_content;
    }

    /**
     * @param  $content
     * @return void
     */
    public function appendContent($content){
        $this->_content .= $content;
    }

}